/*
 * @Date: 2025-02-05 10:28:26
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-02-21 09:01:03
 * @FilePath: /vue3-admin/src/api/role.ts
 */
import { request } from '@/utils';
import type { RoleListItem, RoleDetial } from '@/typings/api/role';

export const readAllRoles = () => {
    return request<HttpResult<RoleListItem[]>>({
        url: `/admin/api/v1/role/readAll`,
        method: 'get'
    }).then((res) => {
        return res.data;
    });
};

export class RoleListParam implements PageParam<keyof RoleListItem> {
    current = 1;
    size = 10;
    name?: string;
}

export const readRoleList = (data: RoleListParam) => {
    return request<HttpResult<PageResult<RoleListItem>>>({
        url: '/admin/api/v1/role/readList',
        method: 'post',
        data
    }).then((res) => {
        return res.data;
    });
};

export const readRoleDetail = (id: number) => {
    return request<HttpResult<RoleDetial>>({
        url: `/admin/api/v1/role/readDetail/${id}`,
        method: 'get'
    }).then((res) => {
        return res.data;
    });
};

export class RoleParam {
    id?: number;
    name = '';
    description = '';
    menuIds = [] as number[];
}

export const createOrUpdateRole = (data: RoleParam) => {
    return request<HttpResult>({
        url: '/admin/api/v1/role/save',
        method: 'post',
        data
    }).then((res) => {
        return res.data;
    });
};

export const deleteRole = (id: number) => {
    return request<HttpResult>({
        url: `/admin/api/v1/role/delete/${id}`,
        method: 'post'
    }).then((res) => {
        return res.data;
    });
};
